<?php

/**
 * 题库管理
 */
namespace app\admin\controller;

use app\admin\model\ArticleCates;
use app\admin\model\Grades;
use app\admin\model\Questions;
use app\admin\model\QuestionTag;
use app\common\controller\AdminBaseController;
use Jrk\Tree;
use think\Exception;
use think\facade\Db;
use think\facade\Route;
use think\Request;
use app\admin\model\Exams;
use app\admin\service\ExcelService;

class Online extends AdminBaseController
{

    protected function initialize()
   {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->model = new Exams();
        //获取所有题
        $examInfo = $this->model->where('status = 1')->select()->toArray();
        $where = [
            ['status' ,'=',  0],
//            ['uid' => self::$admin_info['id']]
        ];
        $gradeModel = new Grades();
        $uGrade = $gradeModel->where('status = 1 and u_id = ' . self::$admin_info['id'])->select()->toArray();
        $grade_eid = array_column($uGrade, 'exam_id');

       foreach ($examInfo as $ek => $ex) {
           if (in_array($ex['id'], $grade_eid)) {
               unset($examInfo[$ek]);
           }
       }

        $this->assign('exam', $examInfo);
    }

    /**
     * @return string
     * @throws \Exception
     * @author: Lucky
     * @describe:index
     */
    public function index(){

        if (IS_AJAX){
            var_dump($this->model->getAdminPageData($this->param));die;
        }

        if (isset($_GET['id']) || !empty($_GET['id'])) {
            $exam_info = $this->model->where('id ='. $_GET['id'])->find()->toArray();
            if (empty($exam_info['qids'])) {
                return '题目出现问题,请联系管理员';
            }
            $ids = json_decode($exam_info['qids'], true);

            $qus = new Questions();
            $qusInfo = $qus->where(['id' => $ids])->select()->toArray();
        }

        if (isset($this->param['type']) && !empty($this->param['type'])) {
            $this->assign('type', $this->param['type']);
        }
        return $this->fetch();
    }

   /**
     * @param Request $request
     * @return string|\think\response\Json
     * @author:
     * @describe:添加
     */
    public function add(Request $request){
        $this->assign("info",[]);
        return $this->fetch();
    }


    /**
     * @param $id
     * @return string|\think\response\Json
     * @author:
     * @describe:编辑
     */
    public function edit($id){

        $info=$this->model->where("id",$id)->find()->toArray();
        if (!$info){
            return parent::failed("未查询到数据");
        }
        $info['tag'] = json_decode($info['tag'], true);

        $this->assign(compact("info"));
        return $this->fetch('add');
    }




    /**
     * @return string
     * @throws \Exception
     * @author: Lucky
     * @describe:回收站
     */
    public function recycle()
    {
        if (IS_AJAX) {
            return $this->model->getRecycleData($this->param);
        }
        return $this->fetch();
    }


    /**
     * @return mixed
     * @author:
     * @describe:真实删除
     */
    public function deltrue()
    {
        if (IS_AJAX) {
            $ids = $this->request->post("ids");

            return $this->model->delTrue($ids);
        }
    }


    /**
     * @return mixed
     * @author:
     * @describe:恢复数据
     */
    public function recycleData()
    {
        if (IS_AJAX) {
            $ids = $this->request->post("ids");
            return $this->model->recycleData($ids);
        }
    }

}